﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq;
using DBContent;
using DBContent.Model;

using System.Linq;
using System.Collections.Generic;
using Confucius.entity;

namespace Confucius.utils
{
    /// <summary>
    /// Author:Liangchuan Gu
    /// date:2012-04-07
    /// </summary>
    public class ConfuciusDBHelper
    {
        private static ConfuciusDB db;
        /// <summary>
        /// Create database
        /// </summary>
        public static void CreateDb()
        {
            if (db == null)
            {
                db = new ConfuciusDB(ConfuciusDB._connStr);
            }
            //  db.DeleteDatabase();
            if (!db.DatabaseExists())
            {
                db.CreateDatabase();
            }

        }

        public static void Add<T>(T record)
        {
            if (record is DBGame)
            {
                DBGame game = new DBGame();
                game.GameID = ((DBGame)(Object)record).GameID;
                game.Status = ((DBGame)(Object)record).Status;
                game.Version = ((DBGame)(Object)record).Version;
                db.Games.InsertOnSubmit(game);
                db.SubmitChanges();
            }
            else if (record is DBItem)
            {
                DBItem item = new DBItem();
                item.ItemID = ((DBItem)(Object)record).ItemID;
                item.GameID = ((DBItem)(Object)record).GameID;
                item.Status = ((DBItem)(Object)record).Status;
                db.Items.InsertOnSubmit(item);
                db.SubmitChanges();
            }
            else if (record is DBNpc)
            {
                DBNpc npc = new DBNpc();
                npc.NpcID = ((DBNpc)(Object)record).NpcID;
                npc.GameID = ((DBNpc)(Object)record).GameID;
                npc.Status = ((DBNpc)(Object)record).Status;
                db.Npcs.InsertOnSubmit(npc);
                db.SubmitChanges();
            }
            else if (record is DBQuest)
            {
                DBQuest quest = new DBQuest();
                quest.QuestID = ((DBQuest)(Object)record).QuestID;
                quest.GameID = ((DBQuest)(Object)record).GameID;
                quest.Status = ((DBQuest)(Object)record).Status;
                quest.StartTime = ((DBQuest)(Object)record).StartTime;
                quest.Duration = ((DBQuest)(Object)record).Duration;
                db.Quests.InsertOnSubmit(quest);
                db.SubmitChanges();
            }
            else if (record is DBTrigger)
            {
                DBTrigger trigger = new DBTrigger();
                trigger.TriggerID = ((DBTrigger)(Object)record).TriggerID;
                trigger.GameID = ((DBTrigger)(Object)record).GameID;
                trigger.Status = ((DBTrigger)(Object)record).Status;
                db.Triggers.InsertOnSubmit(trigger);
                db.SubmitChanges();
            }

        }

        public static void DelDelete<T>(T record)
        {
            if (record is DBGame)
            {
                DBGame oldGame = db.Games.Single(s => s.GameID == ((DBGame)(Object)record).GameID);
                db.Games.DeleteOnSubmit(oldGame);
                db.SubmitChanges();
            }
            else if (record is DBItem)
            {
                DBItem oldItem = db.Items.Single(s => s.ItemID == ((DBItem)(Object)record).ItemID);
                db.Items.DeleteOnSubmit(oldItem);
                db.SubmitChanges();
            }
            else if (record is DBNpc)
            {
                DBNpc oldNpc = db.Npcs.Single(s => s.NpcID == ((DBNpc)(Object)record).NpcID);
                db.Npcs.DeleteOnSubmit(oldNpc);
                db.SubmitChanges();
            }
            else if (record is DBQuest)
            {
                DBQuest oldQuest = db.Quests.Single(s => s.QuestID == ((DBQuest)(Object)record).QuestID);
                db.Quests.DeleteOnSubmit(oldQuest);
                db.SubmitChanges();
            }
            else if (record is DBTrigger)
            {
                DBTrigger oldTrigger = db.Triggers.Single(s => s.TriggerID == ((DBTrigger)(Object)record).TriggerID);
                db.Triggers.DeleteOnSubmit(oldTrigger);
                db.SubmitChanges();
            }

        }

        public static bool IsRecordIn<T>(T record)
        {
            if (record == null)
            {
                return false;
            }

            if (record is DBGame)
            {
                var obj = db.Games.Where(g => g.GameID == ((DBGame)(Object)record).GameID);
                if (obj != null && obj.Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (record is DBItem)
            {
                var obj = db.Items.Where(i => i.ItemID == ((DBItem)(Object)record).ItemID);
                if (obj != null && obj.Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (record is DBNpc)
            {
                var obj = db.Npcs.Where(i => i.NpcID == ((DBNpc)(Object)record).NpcID);
                if (obj != null && obj.Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (record is DBQuest)
            {
                var obj = db.Quests.Where(i => i.QuestID == ((DBQuest)(Object)record).QuestID);
                if (obj != null && obj.Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                var obj = db.Triggers.Where(i => i.TriggerID == ((DBTrigger)(Object)record).TriggerID);
                if (obj != null && obj.Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }

        public static string getGameVersion(DBGame target)
        {
            if (IsRecordIn<DBGame>(target))
            {
                DBGame game = db.Games.Single(s => s.GameID == ((DBGame)(Object)target).GameID);
                if (game != null)
                {
                    return game.Version;
                }
                else
                {
                    return null;
                }
            }
            else 
            {
                return null;
            }

        }

        public static string getGameStatus(DBGame target)
        {
            if (IsRecordIn<DBGame>(target))
            {
                DBGame game = db.Games.Single(s => s.GameID == ((DBGame)(Object)target).GameID);
                if (game != null)
                {
                    return game.Status;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public static void updateGame(DBGame target)
        {
            DBGame oldGame = (from record in db.Games
                              where record.GameID == target.GameID
                              select record).Single();
            if (oldGame != null)
            {
                if (target.Status != null)
                {
                    oldGame.Status = target.Status;
                }
                if (target.Version != null)
                {
                    oldGame.Version = target.Version;
                }
            }

            try
            {
                db.SubmitChanges();
            }
            catch
            {
                throw new Exception("Could not update DBGame record, whose ID is: " + target.GameID);
            }
        }

        public static string getTriggerStatus(DBTrigger target)
        {
            if (IsRecordIn<DBTrigger>(target))
            {
                DBTrigger trigger = db.Triggers.Single(s => s.TriggerID == ((DBTrigger)(Object)target).TriggerID);
                if (trigger != null)
                {
                    return trigger.Status;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public static void updateTriggerStatus(DBTrigger target)
        {
            DBTrigger oldTrigger = (from record in db.Triggers
                              where record.TriggerID == target.TriggerID
                              select record).Single();
            if (oldTrigger != null)
            {
                if (target.Status != null)
                {
                    oldTrigger.Status = target.Status;
                }
            }

            try
            {
                db.SubmitChanges();
            }
            catch
            {
                throw new Exception("Could not update DBTrigger record, whose ID is: " + target.TriggerID);
            }
        }

        public static void updateItemStatus(DBItem target)
        {
            DBItem oldItem = (from record in db.Items
                                    where record.ItemID == target.ItemID
                                    select record).Single();
            if (oldItem != null)
            {
                if (target.Status != null)
                {
                    oldItem.Status = target.Status;
                }
            }

            try
            {
                db.SubmitChanges();
            }
            catch
            {
                throw new Exception("Could not update DBItem record, whose ID is: " + target.ItemID);
            }
        }

        public static string getItemStatus(DBItem target)
        {
            if (IsRecordIn<DBItem>(target))
            {
                DBItem item = db.Items.Single(s => s.ItemID == ((DBItem)(Object)target).ItemID);
                if (item != null)
                {
                    return item.Status;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public static string getNpcStatus(DBNpc target)
        {
            if (IsRecordIn<DBNpc>(target))
            {
                DBNpc npc = db.Npcs.Single(s => s.NpcID == target.NpcID);
                if (npc != null)
                {
                    return npc.Status;
                }
                else
                {
                    return null;
                }
            }
            else 
            {
                return null;
            }

        }

        public static void updateNpcStatus(DBNpc target)
        {
            DBNpc oldNpc = (from record in db.Npcs
                              where record.NpcID == target.NpcID
                              select record).Single();
            if (oldNpc != null)
            {
                if (target.Status != null)
                {
                    oldNpc.Status = target.Status;
                }
            }

            try
            {
                db.SubmitChanges();
            }
            catch
            {
                throw new Exception("Could not update DBNpc record, whose ID is: " + target.NpcID);
            }
        }

        public static string getQuestStatus(DBQuest target)
        {

            if (IsRecordIn<DBQuest>(target))
            {
                DBQuest quest = db.Quests.Single(s => s.QuestID == target.QuestID);
                if (quest != null)
                {
                    return quest.Status;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;

            }

        }

        public static string getQuestDuration(DBQuest target)
        {
            if (IsRecordIn<DBQuest>(target))
            {
                DBQuest quest = db.Quests.Single(s => s.QuestID == ((DBQuest)(Object)target).QuestID);
                if (quest != null)
                {
                    return quest.Duration;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public static string getQuestStartTime(DBQuest target)
        {
            if (IsRecordIn<DBQuest>(target))
            {
                DBQuest quest = db.Quests.Single(s => s.QuestID == ((DBQuest)(Object)target).QuestID);
                if (quest != null)
                {
                    return quest.StartTime;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        public static void updateQuestStatus(DBQuest target)
        {
            DBQuest oldQuest = (from record in db.Quests
                            where record.QuestID == target.QuestID
                            select record).Single();
            if (oldQuest != null)
            {
                if (target.Status != null)
                {
                    oldQuest.Status = target.Status;
                }
                if (target.StartTime != null)
                {
                    oldQuest.StartTime = target.StartTime;
                }
                if (target.Duration != null)
                {
                    oldQuest.Duration = target.Duration;
                }

            }

            try
            {
                db.SubmitChanges();
            }
            catch
            {
                throw new Exception("Could not update DBQuest record, whose ID is: " + target.QuestID);
            }
        }

    }
}
